<?php
namespace App\HttpController;


use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\Http\AbstractInterface\Controller;
USE EasySwoole\Http\Message\Status;


class Index extends Controller
{

    function test(){
        go(function () {
            \co::sleep(0.5);
            echo "hello";
        });
        go("test");

    }

    function index()
    {

        $config = new \EasySwoole\Mysqli\Config([
            'host'          => '127.0.0.1',
            'port'          => 3306,
            'user'          => 'easyswoole',
            'password'      => 'WKEmcBYGexKDa7ez',
            'database'      => 'easyswoole',
            'timeout'       => 5,
            'charset'       => 'utf8mb4',
        ]);

        $client = new \EasySwoole\Mysqli\Client($config);


        $client = new \EasySwoole\Mysqli\Client($config);
        $builder = $client->queryBuilder();

        //构建sql
        //$builder->get('test');
        //执行sql
        //$this->writeJson(Status::CODE_OK, $client->execBuilder());
        //return;





        $data['insert'] = $builder->insert('test', ['name' => "wuying", 'age' => 33]);

       // $data['rows'] = $builder->where('id', 1)->get('test');

//获取最后的查询参数
        $data['options'] =  $builder->getLastQueryOptions();

//获取子查询
        $data['subquery'] = $builder->getSubQuery();


//获取上次条件构造的预处理sql语句
        $data['preparequery'] = $builder->getLastPrepareQuery();
// SELECT  * FROM whereGet WHERE  col1 = ?

//获取上次条件构造的预处理sql语句所以需要的绑定参数
        $data['params'] = $builder->getLastBindParams();
//[2]

//获取上次条件构造的sql语句
        $data['lastquery'] = $builder->getLastQuery();
//SELECT  * FROM whereGet WHERE  col1 = 2

// 获取最后插入的insert_id 使用客户端从swoole mysql获取
        $client->execBuilder();
        $data['insert_id'] = $client->mysqlClient()->insert_id;




        $this->writeJson(Status::CODE_OK, $data);


    }

    protected function actionNotFound(?string $action)
    {
        $this->response()->withStatus(404);
        $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
        if(!is_file($file)){
            $file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
        }
        $this->response()->write(file_get_contents($file));
    }
}
